var base_url_kuaiXuan = '/rs/kuaiXuan';
var base_url_prc2 = '/rs/prc2';

var list = [];
var categories = [];
var operation = "add";
var currentPageNo = 1;
var pageRows = 10;

$(function() {
    authName = getCookie('username');

    queryCategories();

    $("#btnUserQuery").click(function() {
        currentPageNo = 1;
        queryList();
    });
});

function categoryChg(){
    queryList();
}

function queryCategories() {
    zhget(base_url_prc2, {
        pid:0
    }, function(result) {
        var categoryList = $("#tisid");
        var categoryList2 = $("#category");
        categories = result.rows;
        for (var al in categories) {
            var text = categories[al].prcname1;
            var value = categories[al].id;
            categoryList.append("<option value='" + value + "'>" + text + "</option>");
            categoryList2.append("<option value='" + value + "'>" + text + "</option>");
        }
        queryList();
    });
}

function queryList() {
    zhget(base_url_kuaiXuan, {
        category: $('#tisid').val(),
        title: $('#tisname').val(),
        page: currentPageNo,
        size: pageRows
    }, function(result) {
        list = result.rows;
        for(var i = 0; i<list.length; i++) {
            for(var j = 0 ; j < categories.length ; j++){
                if(result.rows[i]['category']==categories[j]["id"]){
                    result.rows[i]['category'] = categories[j]['prcname1'];
                }
             }
        }
        buildTable(result, 'kuaiXuan-template', 'kuaiXuan-placeholder');
    });
}

function onAddClick() {
    cleanForm();
    operation = "add";
    $('#userModal').modal('show');
}

function onUpdateClick(id) {
    fillForm(id);
    operation = "modify";
    $('#userModal').modal('show');
}

function onDeleteClick(id) {
    if(confirm("确认要删除？")) {
        zhdelete(base_url_kuaiXuan + "/" + id, {
            AUTHNAME:authName
        }, function (result) {
            if (result && result.affectedRows > 0) {
                queryList();
                showSuccess('删除成功！');
            } else {
                showError('删除失败！');
            }
        });
    }
}

function onSaveClick() {
    var id = $("#id").val();
    $('#tisid').val($("#category").val());
    var data = {
        AUTHNAME:authName,
        name: $("#name").val(),
        category: $("#category").val(),
        prcid: $("#prcid").val(),
        orderid: $("#orderid").val()
    };
    if (operation == "add") {
        zhpost(base_url_kuaiXuan, data, saveResult);
    } else {
        zhput(base_url_kuaiXuan + "/" + id, data, saveResult);
    }
}

function saveResult(result) {
    if (result) {
        if (result.outcome==0) {
            showError('保存失败！');
        } else{
            $('#userModal').modal('hide');
            queryList();
            showSuccess('保存成功！');
        }
    } else {
        showError('保存失败！');
    }
}

function fillForm(id) {
    var index ;
    for (index in list) {
        var item = list[index];
        if (item.id == id) {
            $("#id").val(item.id);
            $("#name").val(item.name);
            for(var i=0; i<categories.length; i++){
                if(categories[i]['prcname1']===item.category) {
                    $("#category").val(i+1501);
                    break;
                }
            }
            $("#prcid").val(item.prcid);
            $("#orderid").val(item.orderid);
            return;
        }
    }
}

function cleanForm() {
    $("#id").val("");
    $("#name").val("");
    $("#category").val($("#tisid").val());
    $("#prcid").val("");
    $("#orderid").val("9");
}
